#! /usr/bin/python3 python3
# -*- coding: utf-8 -*-

# *************************************************************
#  Projectname @  pysm
#     Filename @  __init__.py
#       Author @  Steve.ma
#  Create date @  2018/8/31
#  Description @  
# *************************************************************

from flask import Flask, request
from Conf import Conf,getConf

class MyApp(Flask):
    def __init__(self):
        Flask.__init__(self, __name__)

    def create_global_jinja_loader(self):
        return self.jinja_loader

    def register_blueprint(self, bp):
        Flask.register_blueprint(self, bp)

# Define the WSGI application object
app = MyApp()
app_conf = getConf('app')

#www
app.url_map.default_subdomain=app_conf['def_subdimain']
#sm.com
app.config['SERVER_NAME'] = app_conf['domain']
# 最大文件上传大小
app.config['MAX_CONTENT_LENGTH'] = app_conf['MAX_FILE_UPLOAD']



# Import a module / component using its blueprint handler variable
from .Admin import admin_blueprint
from .Home import home_blueprint
from .Api import api_blueprint
# from .Hprose import hprose_blueprint

# Register blueprints
app.register_blueprint(admin_blueprint)
app.register_blueprint(home_blueprint)
app.register_blueprint(api_blueprint)
# app.register_blueprint(hprose_blueprint)


@app.before_request
def before_request():
    if request.blueprint is not None:
        bp = app.blueprints[request.blueprint]
        if bp.jinja_loader is not None:
            newsearchpath = bp.jinja_loader.searchpath + app.jinja_loader.searchpath
            app.jinja_loader.searchpath = newsearchpath
            # 以下为2016-03-11日更新：
            # 如果访问非蓝图模块或蓝图中没有指定template_folder,默认使用app注册时指定的全局template_floder.
        else:
            app.jinja_loader.searchpath = app.jinja_loader.searchpath[-1:]
    else:
        app.jinja_loader.searchpath = app.jinja_loader.searchpath[-1:]